//
//  WebViewController.m
//  FaceRecognition
//
//  Created by Sylar on 2016/12/7.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "WebViewController.h"
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"


@interface WebViewController ()<UIWebViewDelegate,UIAlertViewDelegate, NJKWebViewProgressDelegate,UIAlertViewDelegate>
{
    UIWebView * myWebView;
    
    NJKWebViewProgressView *_webViewProgressView;
    NJKWebViewProgress *_webViewProgress;
}

@end

@implementation WebViewController

@synthesize requestURL;
@synthesize controllerTitle;

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])
    {
        [storage deleteCookie:cookie];
    }
    
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    
    
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])
    {
        [storage deleteCookie:cookie];
    }
    
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
    
    [super viewWillDisappear:animated];
}

- (void)back{
    
    if (_type == 1) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navBar.nav_titleLabel.text = self.controllerTitle;
   
   [self.navBar.backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
   
    myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, self.view.frame.size.width, SCREENHEIGH - SafeAreaTopHeight - SafeAreaBottomHeight )];
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:requestURL]];
    myWebView.delegate = self;
    [myWebView loadRequest:request];
    [self.view addSubview:myWebView];
    
    _webViewProgress = [[NJKWebViewProgress alloc] init];
    myWebView.delegate = _webViewProgress;
    _webViewProgress.webViewProxyDelegate = self;
    _webViewProgress.progressDelegate = self;
    
    
    CGRect navBounds = self.navBar.bounds;
    CGRect barFrame = CGRectMake(0,
                                 navBounds.size.height - 2,
                                 navBounds.size.width,
                                 2);
    _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_webViewProgressView setProgress:0 animated:YES];
    
    [self.navBar addSubview:_webViewProgressView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    
    [_webViewProgressView removeFromSuperview];
    
}

#pragma mark -UIWebViewDelegate

-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_webViewProgressView setProgress:progress animated:YES];
    
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    self.navBar.nav_titleLabel.text = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:error.localizedDescription delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
   
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end


